<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <link th:include="common/head :: copy">
</head>
<body style="margin: 1px">
<table id="dg" title="用户管理" class="easyui-datagrid"
       fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
       fit="true" toolbar="#tb">
    <thead>
    <th field="ck" width="20" align="center" checkbox="true"></th>
    <th field="id" width="20" align="center" hidden="false">编号</th>
    <th field="user" width="50" align="center">登录名</th>
    <th field="cname" width="50" align="center">姓名</th>
    <th field="description" width="80" align="center">备注</th>
    <th field="roles" width="150" align="center">拥有角色</th>
    <th field="aa" width="50" align="center" formatter="formatEdit">设置</th>
    </thead>
</table>
<div id="tb">
    <div style="display: inline-block">
        <a href="javascript:openUserAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
    </div>
    <div style="display: inline-block;float: right">
        &nbsp;用户名：&nbsp;<input type="text" id="s_userName" size="20" onkeydown="if(event.keyCode==13) searchUser()"/>
        <a href="javascript:searchUser()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
    </div>
</div>


<div id="dlg" class="easyui-dialog" modal="true" style="width: 450px;height: 300px;padding: 10px 20px"
     closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
    <form id="fm" method="post">
        <input type="hidden" name="id" id="id">
        <table cellspacing="8px">
            <tr>
                <td>用户名：</td>
                <td>
                    <input type="text" id="user" name="user" class="easyui-validatebox" required="true"/>
                </td>
            </tr>
            <tr>
                <td>登录名：</td>
                <td>
                    <input type="text" id="password" name="password" class="easyui-validatebox" required="true" style="width: 250px"/>
                </td>
            </tr>
            <tr>
                <td>真实姓名：</td>
                <td>
                    <input type="text" id="cname" name="cname" class="easyui-validatebox" required="true" style="width: 250px"/>
                </td>
            </tr>
            <!--				<tr>-->
            <!--					<td valign="top">备注：</td>-->
            <!--					<td>-->
            <!--						<textarea rows="5" cols="40" id="remarks" name="remarks"></textarea>-->
            <!--					</td>-->
            <!--				</tr>-->
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveUser()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
    <a href="javascript:closeUserDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
</div>


<div id="dlg2" class="easyui-dialog" modal="true" style="width: 500px;height: 350px;"
     closed="true" buttons="#dlg-buttons2">
    <input type="hidden" id="userId" name="userId"/>
    <table id="dg2"  class="easyui-datagrid" fitColumns="true" fit="true">
        <thead>
        <th field="cb" checkbox="true" align="center"></th>
        <th field="id" width="20" align="left" hidden="true">编号</th>
        <th field="cname" width="100" align="left">角色名称</th>
        <th field="description" width="200" align="left">备注</th>
        </thead>
    </table>
</div>

<div id="dlg-buttons2">
    <a href="javascript:saveRoleSet()" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
    <a href="javascript:closeRoleSetDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
</div>
</body>
<script type="text/javascript">

    function formatEdit(val,row,index){
        str="<a href=\"javascript:setFun('"+row.roles+"',"+row.id+")\">设置</a>"
        str+="<a href=\"javascript:editFun("+index+")\">修改</a>"
        str+="<a href=\"javascript:deleteFun("+row.id+")\">删除</a>"
        return str;
    }

    function setFun(roles,userId){
        roles=roles||'';
        var rolesArr=roles.split(",");
        $("#dlg2").dialog("open").dialog("setTitle","选择角色");
        $("#dg2").datagrid({
            url:'/sys/role/getAll',
            method:'get',
            onLoadSuccess:function(data){
                var allRows=$("#dg2").datagrid("getRows");
                for(var i=0;i<allRows.length;i++){
                    var name=allRows[i].cname;
                    if($.inArray(name,rolesArr)>=0){
                        $("#dg2").datagrid("checkRow",i);
                    }
                }
            }
        });
        $("#userId").val(userId);
    }

    function searchUser(){
        $("#dg").datagrid('load',{
            'name':$("#s_userName").val()
        });
    }

    function resetValue(){
        $("#userName").val("");
        $("#password").val("");
        $("#trueName").val("");
        $("#remarks").val("");
    }

    function closeUserDialog(){
        $("#dlg").dialog("close");
        resetValue();
    }

    function saveUser(){
        $("#fm").form("submit",{
            url:"/fm/user",
            onSubmit:function(){
                return $(this).form("validate");
            },
            success:function(result){
                var result=eval('('+result+')');
                if(result.success){
                    message("保存成功!")
                    // $.messager.alert("系统提示","保存成功！");
                    // resetValue();
                    $("#dlg").dialog("close");
                    $("#dg").datagrid("reload");
                }else{
                    message(result.msg)
                    // $.messager.alert("系统提示",result.errorInfo);
                }
            }
        });
    }

    function openUserAddDialog(){
        $("#dlg").dialog("open").dialog("setTitle","添加用户信息");
        $('#fm').form('clear');
    }

    function editFun(index){
        $('#dg').datagrid("clearSelections");
        $('#dg').datagrid("selectRow",index);
        var row=$("#dg").datagrid("getSelected");
        // if(selectedRows.length!=1){
        // 	$.messager.alert("系统提示","请选择一条要修改的数据！");
        // 	return;
        // }
        // var row=selectedRows[0];
        $("#dlg").dialog("open").dialog("setTitle","修改用户信息");
        $("#fm").form("load",row);
        // $("#userName").attr("readonly","readonly");
        // url="/admin/user/save?id="+row.id;
    }

    function deleteFun(id){
        $.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
            if(r){
                $.post("/fm/user/delete",{id:id},function(result){
                    if(result.success){
                        // $.messager.alert("系统提示","数据已成功删除！");
                        message("数据已成功删除");
                        $("#dg").datagrid("reload");
                    }else{
                        message(result.msg);
                    }
                },"json");
            }
        });
        // var selectedRows=$("#dg").datagrid("getSelections");
        // if(selectedRows.length!=1){
        // 	$.messager.alert("系统提示","请选择一条要删除的数据！");
        // 	return;
        // }
        // var id=selectedRows[0].id;
        // $.messager.confirm("系统提示","您确定要删除这条数据吗?",function(r){
        // 	if(r){
        // 		$.post("/admin/user/delete",{id:id},function(result){
        // 			if(result.success){
        // 				$.messager.alert("系统提示","数据已成功删除！");
        // 				$("#dg").datagrid("reload");
        // 			}else{
        // 				$.messager.alert("系统提示",result.errorInfo);
        // 			}
        // 		},"json");
        // 	}
        // });
    }

    $(document).ready(function() {

        $("#dg").datagrid({
            url:"/fm/user",
            method:'get',
            onDblClickRow:function(index,row){
                $("#dlg").dialog("open").dialog("setTitle","修改用户信息");
                $("#fm").form("load",row);
                $("#userName").attr("readonly","readonly");
                url="/admin/user/save?id="+row.id;
            }
        });

    });


    function saveRoleSet(){
        var userId=$("#userId").val();
        var selectedRows=$("#dg2").datagrid("getSelections");
        var strRoleIds=[];
        var strRoleNames=[];
        for(var i=0;i<selectedRows.length;i++){
            strRoleIds.push(selectedRows[i].id);
            strRoleNames.push(selectedRows[i].cname);
        }
        var roleIds=strRoleIds.join(",");
        $.post("/sys/user/saveRoleSet",{roleIds:roleIds,roleNames:strRoleNames.join(","),userId:userId},function(result){
            if(result.success){
                closeRoleSetDialog();
                $("#dg").datagrid("reload");
            }else{
                message("系统提示","提交失败，请联系管理员！");
                // $.messager.alert("系统提示","提交失败，请联系管理员！");
            }
        },"json");
    }

    function closeRoleSetDialog(){
        $("#dlg2").dialog("close");
    }


</script>
</html>